package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("setmeal")
public class SetmealController {

    @Autowired
    private SetmealService service;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private DishService dishService;

    @GetMapping("/page")
    public R<Page<SetmealDto> > getSetmealsByPage(Integer page,Integer pageSize,String name){
        Page<Setmeal> setmealPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal> wrapper= new LambdaQueryWrapper<>();
        wrapper.like(name != null,Setmeal::getName,name);
        //根据分页信息+条件查询 获得 相对应的 套餐列表
        service.page(setmealPage,wrapper);
        //获取分页对象中的 套餐集合数据
        List<Setmeal> setmealList=setmealPage.getRecords();
        //新建套餐数据模型的 集合 用于 增强 原有套餐数据
        List<SetmealDto> setmealDtoList = new ArrayList<>();
        //遍历原有集合用于 添加套餐名称
        for (Setmeal setmeal : setmealList) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal,setmealDto);
            //根据套餐中 的 所属套餐类型的ID 找到 套餐分类名称
            setmealDto.setCategoryName(categoryService.getById(setmeal.getCategoryId()).getName());
            //将赋值后的 新套餐模型 放到 集合中
            setmealDtoList.add(setmealDto);
        }

        Page<SetmealDto> setmealDtoPage = new Page<>();
        //将分页数据 进行 赋值给 新的 增强套餐 的 分页对象中
        BeanUtils.copyProperties(setmealPage,setmealDtoPage);
        //设置封装后的 新套餐数据模型 放到 分页数据源中
        setmealDtoPage.setRecords(setmealDtoList);
        return R.success(setmealDtoPage);
    }

    @PostMapping
    @CacheEvict(value="setmealCache",key="#setmealDto.categoryId")
    public R<String> addSetmealWithDish(@RequestBody SetmealDto setmealDto){
        Boolean flag = service.addSetmelWithDish(setmealDto);
        return R.success("");
    }

    @GetMapping("/{id}")
    public R<SetmealDto> getSetmealWithDishById(@PathVariable Long id){
        SetmealDto setmealDto = service.getSetmealWithDishById(id);
        return R.success(setmealDto);
    }

    @PostMapping("/status/{status}")
    @CacheEvict(value="setmealCache",allEntries=true)
    public R<String> changeSetmealStatusByIds(@PathVariable Integer status, Long[] ids) {
        List<Setmeal> setmeals=new ArrayList<>();
        for (Long id : ids) {
            Setmeal setmeal=new Setmeal();
            setmeal.setStatus(status);
            setmeal.setId(id);
            setmeals.add(setmeal);
        }
        service.updateBatchById(setmeals);
        return R.success("修改状态成功!");
    }

    @PutMapping
    @CacheEvict(value="setmealCache",key="#setmealDto.categoryId")
    public R<String> updateSetmealWithDish(@RequestBody SetmealDto setmealDto){
       Boolean flag =  service.updateSetmealWithDish(setmealDto);
       return flag ? R.success("更新套餐成功") : R.error("更新套餐失败");
    }

    @DeleteMapping
    @CacheEvict(value="setmealCache",allEntries=true)
    public R<String> deleteSetmealByIds(@RequestParam List<Long> ids){
        service.removeByIds(ids);
        return R.success("删除套餐成功");
    }

    @Cacheable(value="setmealCache",key="#setmeal.categoryId")
    @GetMapping("/list")
    public R<List<Setmeal>> getSetmealListByCategoryId(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getCategoryId,setmeal.getCategoryId());
        List<Setmeal> list=service.list(queryWrapper);
        return R.success(list);
    }

    @GetMapping("/dish/{id}")
    public R<List<Dish>> getSetmealById(@PathVariable Long id){

        Setmeal setmeal=service.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list=setmealDishService.list(lqw);
        List<Dish> dishList = new ArrayList<>();
        for (SetmealDish setmealDish : list) {
            Dish dish=dishService.getById(setmealDish.getDishId());
            dishList.add(dish);
        }
        setmealDto.setSetmealDishes(list);
        return R.success(dishList);
    }

}
